﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RhythmComet.Utils;

namespace RhythmComet.Utils
{
    public class CollisionHelper
    {
        public static bool LineCircleCollision(Vector3 begin, Vector3 end, Vector3 center, float radius)
        {
            Vector3 r = end - begin;
            Vector3 s = center - begin;
            float rLength = r.LengthSquared();
            if (rLength < 0f)
                return false;
            float projectionLentgh = Vector3.Dot(r, s);
            projectionLentgh /= rLength;
            if (projectionLentgh < 0.0f)
                projectionLentgh = 0.0f;
            else if (projectionLentgh > 1.0f) // isso é possível?
                projectionLentgh = 1.0f;
            Vector3 projection = begin + projectionLentgh * r;
            Vector3 result = center - projection;
            if (result.LengthSquared() < (radius * radius))
                return true;
            return false;
        }
    }
}
